package main

import (
	tebeaweb "aiAutoPilot/robot"
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
	"time"
)

func main() {

	// 创建一个执行Playwright版本的命令
	cmd := exec.Command("playwright", "--version")

	// 运行命令并捕获输出
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()

	// 检查命令是否成功执行
	if err != nil {
		fmt.Println("Playwright is not installed or not in PATH.")

		cmdInstall := exec.Command("playwright", "install")
		if err := cmdInstall.Run(); err != nil {
			log.Fatalf("Failed to install Playwright browsers: %v", err)
		}
	}

	reader := bufio.NewReader(os.Stdin) // 创建一个新的bufio.Reader，用于读取标准输入

	fmt.Println("欢迎使用自动任务机器人")
	fmt.Println("1. 未读新闻自动处理")
	fmt.Println("2. 我司达人自动学习")

	for {
		fmt.Print("请选择任务：")
		command, err := reader.ReadString('\n') // 读取用户输入的命令
		if err != nil {
			fmt.Println("读取输入错误：", err)
			return
		}

		//command = command[:len(command)-1] // 去除末尾的换行符
		command = strings.ReplaceAll(command, "\r\n", "")
		switch command {
		case "1":
			fmt.Println("请输入要检查的月份-格式如：202410")
			monthstr, err := reader.ReadString('\n')
			if err != nil {
				fmt.Println("读取输入错误：", err)
				return
			}

			monthstr = monthstr[:len(monthstr)-1] // 去除末尾的换行符

			tebeaweb.RunTbeaWebRobot("checkNews", monthstr)
			fmt.Println("特变新闻处理完成....")
			time.Sleep(12000 * time.Second)
		case "exit":
			fmt.Println("程序退出。")
			return
		default:
			fmt.Println("未知命令：", command)
		}
	}

	fmt.Println("特变新闻处理完成....")
	time.Sleep(12000 * time.Second)
}
